package com.example.demo.arithmetic.algorithm.sort2024;

public class CheckSortedArray {
    public static boolean isSorted(int[] arr) {
        if (arr.length <= 1) {
            return true; // 数组为空或仅有一个元素，认为有序
        }

        boolean isAscending = true;
        boolean isDescending = true;

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < arr[i - 1]) {
                isAscending = false;
            }
            if (arr[i] > arr[i - 1]) {
                isDescending = false;
            }
        }

        return isAscending || isDescending;
    }

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {5, 4, 3, 2, 1};
        int[] arr3 = {1, 3, 2, 4, 5};

        System.out.println(isSorted(arr1)); // 输出: true
        System.out.println(isSorted(arr2)); // 输出: true
        System.out.println(isSorted(arr3)); // 输出: false
    }
}
